package com.xiaolin.io;

import java.io.*;

/**
 * @ClassName TestDataInputStream
 * @Description TODO
 * @Detail 注意： DataOutputStream输出数据到文件，那么DataInputStream要按顺序来读取数据
 * @Author MyPC
 * @Date 2020/9/26
 * @Version 1.0
 */
public class TestDataInputStream {
    public static void main(String[] args) throws IOException {
        testWriteAndRead();
    }

    /**
     * 测试读写数据
     */
    public static void testWriteAndRead(){
        DataOutputStream dataOutputStream= null;
        DataInputStream dataInputStream=null;
        try {
            dataOutputStream = new DataOutputStream(new FileOutputStream("D:\\1file\\data.txt"));
            dataInputStream=new DataInputStream(new FileInputStream("D:\\1file\\data.txt"));
            dataOutputStream.writeBoolean(true);
            dataOutputStream.writeChar('h');
            boolean b = dataInputStream.readBoolean();
            char c = dataInputStream.readChar();
            System.out.println(b);
            System.out.println(c);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(dataOutputStream != null){
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(dataInputStream !=null){
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
